// package headfirst.designpatterns.observer.weather;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StatisticsDisplay implements ActionListener, DisplayElement {
	private float maxTemp = 0.0f;
	private float minTemp = 200;
	private float tempSum= 0.0f;
	private int numReadings;
	// private WeatherData weatherData;

    @Override
	public  void actionPerformed(ActionEvent event) {
		WeatherData wd = (WeatherData)(event.getSource());
		update(wd.getTemperature(), wd.getHumidity(), wd.getPressure());
	}
	public StatisticsDisplay(WeatherData weatherData) {
		// this.weatherData = weatherData;
		// weatherData.registerObserver(this);
		weatherData.addActionListener(this);
	}

	public void update(float temp, float humidity, float pressure) {
		tempSum += temp;
		numReadings++;

		if (temp > maxTemp) {
			maxTemp = temp;
		}
 
		if (temp < minTemp) {
			minTemp = temp;
		}

		display();
	}

	public void display() {
		System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
			+ "/" + maxTemp + "/" + minTemp);
	}
}
